import { Component, OnInit, Input } from '@angular/core';
import { DialogService } from 'primeng/primeng';
import { MdlSnackbarService } from '@angular-mdl/core';
import { ConfirmationService } from 'primeng/primeng';
import { FreeflowService } from '../freeflow.service';
import { FfTransetAttrComponent } from './fftranset-attr.component';

@Component({
  selector: 'ff-transet',
  templateUrl: './fftranset.component.html',
  styleUrls: ['./fftranset.component.scss']
})
export class FfTransetComponent implements OnInit {
  currentActDefId: number;
  @Input()
  set procDefId(procDefId: number) {
    if (!procDefId) return;
    this.currentActDefId = procDefId;
    this.freeflowService.listProcTrans(procDefId).subscribe(trans => this.trans = trans);
  }
  get procDefId(): number { return this.currentActDefId; }

  trans: any[];
  selectedTran: any;
  cols: any[] = [
    { field: 'transId', header: '编号', rowspan: 1, style: { 'width': '10%', 'text-align': 'center' } },
    { field: 'transName', header: '名称', rowspan: 1, style: { 'width': '20%', 'text-align': 'center' } },
    { field: 'fromActName', header: '开始节点', rowspan: 1, style: { 'width': '20%', 'text-align': 'center' } },
    { field: 'toActName', header: '结束节点', rowspan: 1, style: { 'width': '20%', 'text-align': 'center' } }
  ];

  constructor(
    private freeflowService: FreeflowService,
    private dialogService: DialogService,
    private mdlSnackbarService: MdlSnackbarService,
    private confirmationService: ConfirmationService) {
  }

  ngOnInit() {
  }

  /**移除流向信息 */
  remove(data: any) {
    this.confirmationService.confirm({
      header: '确认',
      message: '您确定要删除该条流向吗？',
      accept: () => {
        this.freeflowService.deleteProcTran(data['transId']).subscribe(data => {
          this.freeflowService.listProcTrans(this.currentActDefId).subscribe(trans => this.trans = trans);
          this.mdlSnackbarService.showToast('数据删除成功！');
        });
      }
    });
  }

  /**编辑属性 */
  editAttribute($event) {
    const ref = this.dialogService.open(FfTransetAttrComponent, {
      header: '流向属性',
      width: '450px',
      contentStyle: { 'height': '450px' },
      data: {
        'transId': this.selectedTran.transId
      }
    });
    ref.onClose.subscribe(data => {
      if (data) {
        this.freeflowService.listProcTrans(this.currentActDefId).subscribe(trans => this.trans = trans);
        this.mdlSnackbarService.showToast('保存成功！');
      }
    });
  }

  refresh() {
    this.freeflowService.listProcTrans(this.procDefId).subscribe(trans => this.trans = trans);
  }
}
